package com.wejoy.video.middle.station.data.service.workorder.impl;

import com.wejoy.video.middle.station.data.domain.workorder.OrderAlarmAssort;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.workorder.IOrderAlarmAssortMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.workorder.IOrderAlarmAssortService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单告警分类
 *
 * @author miracle_j
 * @date 2022年11月27日 21:57:58
 */
@Service
public class OrderAlarmAssortServiceImpl implements IOrderAlarmAssortService {
    @Autowired
    private IOrderAlarmAssortMapper orderAlarmAssortMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(OrderAlarmAssort req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(OrderAlarmAssort req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return orderAlarmAssortMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(OrderAlarmAssort orderAlarmAssort) {
        WJAssert.notNull(orderAlarmAssort, "入参对象不能为空");
        WJAssert.notNull(orderAlarmAssort.getId(), "id参数错误");
        OrderAlarmAssort where = new OrderAlarmAssort();
        where.setId(orderAlarmAssort.getId());
        return orderAlarmAssortMapper.updateSelective(orderAlarmAssort, where);
    }

    @Override
    public int getCount(OrderAlarmAssort orderAlarmAssort) {
        checkStringLength(orderAlarmAssort, true);
        return orderAlarmAssortMapper.getCount(orderAlarmAssort);
    }

    @Override
    public OrderAlarmAssort getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        OrderAlarmAssort where = new OrderAlarmAssort();
        where.setId(id);
        return orderAlarmAssortMapper.getOne(where);
    }

    @Override
    public List<OrderAlarmAssort> getList(OrderAlarmAssort orderAlarmAssort) {
        checkStringLength(orderAlarmAssort, true);
        return orderAlarmAssortMapper.getList(orderAlarmAssort);
    }

    @Override
    public List<OrderAlarmAssort> getPageList(OrderAlarmAssort orderAlarmAssort) {
        checkStringLength(orderAlarmAssort, true);
        return orderAlarmAssortMapper.getPageList(orderAlarmAssort);
    }

    @Override
    public void audit(Long id, String operator) {
        OrderAlarmAssort req = new OrderAlarmAssort();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        OrderAlarmAssort req = new OrderAlarmAssort();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        OrderAlarmAssort req = new OrderAlarmAssort();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(OrderAlarmAssort orderAlarmAssort, boolean nullAble) {
        WJAssert.notNull(orderAlarmAssort, "入参对象不能为空");
        WJAssert.limitMaxLength(orderAlarmAssort.getName(), nullAble, 50, "分类名称不合法!");
        WJAssert.limitMaxLength(orderAlarmAssort.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(orderAlarmAssort.getRemark(), true, 255, "备注不合法!");
    }
}
